package com.zxx.pattern.prototypePattern.prototype2_ZZ;

import java.util.Hashtable;

/**
 * 步骤 3
 * 创建一个类，从数据库获取实体类，并把它们存储在一个 Hashtable 中。
 *
 * @author zxx on 2020/12/21
 */
public class ShapeCache2 {

    private static Hashtable<String, Shape2> shapeMap = new Hashtable<>();

    public static Shape2 getShape(String shapeId) {
        Shape2 shape2 = shapeMap.get(shapeId);
        return (Shape2) shape2.clone();
        //return shape2;
    }

    //对每种形状都运行数据库查询，并创建该形状
    //shapeMap.put(key, shape2)
    //例如，我们要添加三种形状
    public static void loadShape() {
        Circle2 circle2 = new Circle2();
        circle2.setId("1");
        shapeMap.put(circle2.getId(), circle2);

        Square2 square2 = new Square2();
        square2.setId("2");
        shapeMap.put(square2.getId(), square2);

        Rectangle2 rectangle2 = new Rectangle2();
        rectangle2.setId("3");
        shapeMap.put(rectangle2.getId(), rectangle2);
    }


}
